using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Specialized;
using System.Text.RegularExpressions;
using System.Xml;

namespace CodePlex.Resourceful.Examples
{
    public static class GDataExample
    {

        public static void Run()
        {



            // google account info
            string info = Environment.GetEnvironmentVariable("GOOGLE");

            string email = info.Split(':')[0];
            string password = info.Split(':')[1];



            // client
            Client client = new Client(new Uri("https://www.google.com/accounts/"));
            client.Tracer.AddListener(Console.Out);
   


            // get auth token from google accounts
            NameValuePairs parameters = new NameValuePairs();
            parameters["accountType"] = "GOOGLE";
            parameters["Email"] = email;
            parameters["Passwd"] = password;
            parameters["service"] = "gbase";
            parameters["source"] = "CodePlex-Resourceful-" + typeof(Client).Assembly.GetName().Version.ToString(3);


            string response = client.PostRepresentation("ClientLogin", MediaType.ApplicationExWwwFormUrlencoded, parameters).AsString();
            string auth = Regex.Match(response, "Auth=(.*)$", RegexOptions.Singleline).Groups[1].Value;

           
            // all subsequent requests should contain the google auth header
            client.CommonHeaders[HttpHeader.Authorization] = string.Format("GoogleLogin auth={0}", auth);



            // switch over to the gbase root
            client.RootUri = new Uri("http://www.google.com/base/");




            // insert example item (recipe)

            string item = @"   
<entry xmlns='http://www.w3.org/2005/Atom'
  xmlns:g='http://base.google.com/ns/1.0'>
  <category scheme='http://base.google.com/categories/itemtypes' term='Recipes'/>
  <title type='text'>He Jingxian's chicken</title>
  <content type='xhtml'>
    <div xmlns='http://www.w3.org/1999/xhtml'>Delectable Sichuan specialty</div>
  </content>
  <link rel='alternate' type='text/html' href='http://www.host.com/123456jsh9'/>
  <g:label type='text'>kung pao chicken</g:label>
  <g:label type='text'>chinese cuisine</g:label>
  <g:label type='text'>recipes</g:label>
  <g:label type='text'>asian</g:label>
  <g:label type='text'>sichuan</g:label>
  <g:location type='location'>Mountain View, CA 94043</g:location>
  <g:item_type type='text'>Recipes</g:item_type>
  <g:cooking_time type='intUnit'>30 minutes</g:cooking_time>
  <g:main_ingredient type='text'>chicken</g:main_ingredient>
  <g:main_ingredient type='text'>chili peppers</g:main_ingredient>
  <g:main_ingredient type='text'>peanuts</g:main_ingredient>
  <g:serving_count type='number'>5</g:serving_count>
</entry>
";


            string location = client.PostRepresentation("feeds/items", MediaType.ApplicationAtomXml, item).Headers[HttpHeader.Location];

            
            // get items (new item should be included)
            DumpEntries(client);


            // delete example item
            client.DeleteRepresentation(location);

            // get items (new item should not be included)
            DumpEntries(client);


            // delete all items
            //XmlDocument doc = client.GetRepresentation("feeds/items").AsXmlDocument();
            //XmlNamespaceManager ns = Utils.GetNamespaceManager(doc, "a");
            //foreach (XmlNode n in doc.SelectNodes("//a:feed/a:entry/a:id", ns))
            //{
            //    client.DeleteRepresentation(n.InnerText);

            //}


            Utils.Prompt();


        }


        private static void DumpEntries(Client client)
        {

            XmlDocument doc = client.GetRepresentation("feeds/items").AsXmlDocument();

            Console.WriteLine("Entries:");

            XmlNamespaceManager ns = Utils.GetNamespaceManager(doc, "a");
          
            foreach (XmlNode n in doc.SelectNodes("//a:feed/a:entry", ns))
            {
                Console.WriteLine("  Entry id: " + n.SelectSingleNode("a:id",ns).InnerText);
                Console.WriteLine("    Item Type: " + n.SelectSingleNode("g:item_type", ns).InnerText);
                Console.WriteLine("    Title: " + n.SelectSingleNode("a:title", ns).InnerText);
              
            }
        }



        


      
    }
}
